﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.KhoaHocService;
namespace eTMS.Controllers{
public class KhoaHocControllerBase : CommonController{
	public KhoaHocControllerBase()
		: base()
	{	
		this.dao = new KhoaHocServiceClient();
	}
	~KhoaHocControllerBase(){
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	private DataTable _Table;
	private KhoaHocEntity oKhoaHoc;	
	public KhoaHocEntity KhoaHocObject{
		get{
			return oKhoaHoc;
		}
		set{
			oKhoaHoc=value;
		}
	}
	protected KhoaHocServiceClient dao;
	public DataTable Table{
		get{
			this.LoadAllDSKhoaHoc(false);			
			return _Table;
		}
	}
	public void LoadAllDSKhoaHoc(bool clearCache){
		bool bForce=clearCache;
		if(_Table==null)
		{
			bForce=true;
		}
		if(!bForce)return;
		DataSet ds=dao.SelectAllDS();
		if(ds.Tables.Count>0){
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
		}
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oKhoaHoc==null)return false;
		bool result=true;
		try{
			oKhoaHoc = dao.SaveRet(oKhoaHoc);
		}
		catch{
			result=false;
		}
		if(result){
			_ListKhoaHoc.Add(oKhoaHoc);
		}
		return result;
	}
	public bool Update(){
		if(oKhoaHoc==null)return false;
		return dao.Update(oKhoaHoc);
	}
	public bool Delete(){
		if(oKhoaHoc==null)return false;
		bool retvalue = dao.Delete(oKhoaHoc);
		if(retvalue){			
			_ListKhoaHoc.Remove(oKhoaHoc);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 KhoaHoc_ID){
		bool retvalue = dao.DeleteByID(KhoaHoc_ID);		
		if(retvalue){
			KhoaHocEntity oDeleted=FindKhoaHoc(KhoaHoc_ID);
			if(oDeleted!=null){
				_ListKhoaHoc.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oKhoaHoc = new KhoaHocEntity();
	}
	//Check if exists MaKhoaHoc
	public bool CheckExistsMaKhoaHoc(object value){	
		return dao.SelectOneByMaKhoaHoc(value.ToString()).MaKhoaHoc!=null;
    }    
    //Copy data from data row in grid to object
    public KhoaHocEntity MapDataRowToObject(DataRow row){
		KhoaHocEntity o = new KhoaHocEntity();
		 o.MaKhoaHoc=Convert.ToString(row["MaKhoaHoc"]);
		 o.TenKhoaHoc=Convert.ToString(row["TenKhoaHoc"]);
		 o.MoTaKhoaHoc=Convert.ToString(row["MoTaKhoaHoc"]);
		 if(!row.IsNull("BatDau"))
			o.BatDau=ConvertDateTime(row["BatDau"]);
		 if(!row.IsNull("KhoaHoc_ID"))
		    o.KhoaHoc_ID=(Int32)(row["KhoaHoc_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(KhoaHocEntity o,DataRow row){
		row["MaKhoaHoc"]=o.MaKhoaHoc;
		row["TenKhoaHoc"]=o.TenKhoaHoc;
		row["MoTaKhoaHoc"]=o.MoTaKhoaHoc;
		row["BatDau"]=o.BatDau;
		row["KhoaHoc_ID"]=o.KhoaHoc_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.KhoaHocObject,row);
    }
	public void LoadObject(Int32 KhoaHoc_ID){
		KhoaHocEntity oLoad=FindKhoaHoc(KhoaHoc_ID);
		if(oLoad==null){
			oKhoaHoc=this.LoadKhoaHoc(KhoaHoc_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oKhoaHoc.IsNullKhoaHoc_ID;
			if(!bNotIsNull){
				this.ListKhoaHoc.Add(oKhoaHoc);
			}
		}
		else{
			oKhoaHoc=oLoad;
		}
	}
	#region CachedList KhoaHoc
	private static List<KhoaHocEntity> _ListKhoaHoc=new List<KhoaHocEntity>();
    public List<KhoaHocEntity>ListKhoaHoc{
		get{
			return _ListKhoaHoc;
		}
	}
	public static List<KhoaHocEntity>ListKhoaHocStatic{
		get{
			return _ListKhoaHoc;
		}
	}
	public KhoaHocEntity FindKhoaHoc(Int32 KhoaHoc_ID){
		KhoaHocEntity oResult=this.ListKhoaHoc.Find(delegate(KhoaHocEntity oFind){
			bool result=true; 
			result&=oFind.KhoaHoc_ID.Equals(KhoaHoc_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllKhoaHoc(bool clearCache){
		if(_ListKhoaHoc.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		KhoaHocServiceClient dao = new KhoaHocServiceClient();
		_ListKhoaHoc=new List<KhoaHocEntity>(dao.SelectAll());
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	public KhoaHocEntity LoadKhoaHoc(Int32 KhoaHoc_ID){			
		KhoaHocEntity o=dao.SelectOne(KhoaHoc_ID);		
		return o;
	}
	#endregion
}
}
